package com.face.mapper;

import com.face.entity.Account;
import com.face.wrapper.AccountWrapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AccountMapper {

    @Select("select a.*, b.permissions from tb_account a left join tb_role b on a.roleId = b.id where username = #{username}")
    AccountWrapper findByUsername(String username);

    @Insert("insert into tb_account (username, password, shopId, createTime, updateTime, email, activeCode, status, roleId, type) values(#{username}, #{password}, #{shopId}, #{createTime}, #{updateTime}, #{email}, #{activeCode}, #{status}, #{roleId}, #{type})")
    void save(Account account);

    @Update("update tb_account set activeCode = '', status = 2 where activeCode = #{activeCode}")
    void active(String activeCode);

    @Select("select * from account where activeCode = #{activeCode}")
    Account findByActiveCode(String activeCode);

    @Select("select * from tb_account where email = #{email}")
    Account findByEmail(String email);

    @Select("select a.username, a.realname, a.createTime, b.name role from tb_account a left join tb_role b on a.roleId = b.id where a.shopId = #{shopId} order by ${order}")
    List<AccountWrapper> listAll(@Param("shopId")String shopId, @Param("order")String order);

    @Select("select a.username, a.realname, a.createTime, b.name role from tb_account a left join tb_role b on a.roleId = b.id  where a.shopId = #{shopId} and roleId=#{roleId} and a.username like concat(concat('%', #{username}), '%') and a.realname like concat(concat('%', #{realname}), '%') order by ${order}")
    List<AccountWrapper> listByRoleId(@Param("shopId")String shopId, @Param("username")String username, @Param("realname")String realname, @Param("roleId") Integer roleId, @Param("order")String order);

    @Select("select a.username, a.realname, a.createTime, b.name role from tb_account a left join tb_role b on a.roleId = b.id  where a.shopId = #{shopId} and a.username like concat(concat('%', #{username}), '%') and a.realname like concat(concat('%', #{realname}), '%') order by ${order}")
    List<AccountWrapper> list(@Param("shopId")String shopId, @Param("username")String username, @Param("realname")String realname,@Param("order")String order);

    @Insert("insert into tb_account(shopId, username,password,createTime,realname,roleId,activeCode,status,type) values(#{shopId},#{username},#{password},#{createTime},#{realname},#{roleId},'',0, #{type})")
    Integer saveAccount(Account account);

    @Update("update tb_account set realname=#{realname},roleId=#{roleId},password=#{password} where username=#{username}")
    Integer updateAccount(Account account);

    @Update("update tb_account set password=123456 where username=#{username}")
    Integer resetPassword(String username);

    @Delete("delete from tb_account where username=#{username}")
    Integer deleteAccount(String username);

    @Select("select a.* from tb_account a where a.shopId = #{shopId} and a.type = 1")
    Account findManagerByShopId(String shopId);

    @Select("select a.openid from tb_account a left join tb_role b on a.roleId = b.id where a.shopId = #{shopId} and a.openid is not null and b.enable_notify = 0")
    List<String> findEnableNotifyOpenids(String shopId);

    @Update("update tb_account set password = #{newPassword} where username = #{username}")
    void changePassword(String username, String newPassword);

    @Update("update tb_account set status = #{status} where shopId = #{shopId}")
    void updateStatus(String shopId, int status);
    @Update("update tb_account set status = #{status} where username = #{username}")
    void updateStatusByUsername(String username, int status);

    @Update("update tb_account set openid=#{openid} where username=#{username}")
    Integer updateAccountWeixin(String username,String openid );

    @Select("select username from tb_account where openid = #{openid}")
    String findUsernameByOpenid(String openid);

    @Select("select username, realname from tb_account where shopId = #{shopId} and realname!=''")
    List<Account> findAllByShopId(String shopId);

    @Select("select username from tb_account where roleId = #{roleId}")
    List<Account> findByRoleId(Integer roleId);
}
